﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RobotEscape.Screens;

namespace RobotEscape
{

    // HELLO!  I see you are reading my source.
    // I'm so sorry.

    public class Game
    {
        public int CANVAS_WIDTH = 960;
        public int CANVAS_HEIGHT = 640;
        public int LEVEL_COUNT = 10;
        public Random rand = new Random();
        public ScreenBase currentScreen = null;
        public Canvas Canvas;
        Entities.MessageBox mbFocus;
        public GameTime currentGameTime;
        DateTime lastUpdate;
        public bool hasSeenHelp = false;
        public bool hasFocus = false;


        #region Singleton
        private static Game _current = null;
        public static Game Instance
        {
            get { return _current ?? (_current = new Game()); }
        }
        #endregion

        public Game()
        {
            currentGameTime = new GameTime();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            var now = DateTime.Now;
            currentGameTime.Update(now - lastUpdate);
            lastUpdate = now;
            if (this.currentScreen != null)
                this.currentScreen.Update(currentGameTime);
            KeyState.Update();
        }


        public void SwitchScreen(ScreenBase screen, bool removeCurrent = true)
        {
            //Canvas.Children.Clear();
            if (removeCurrent && currentScreen != null)
                currentScreen.Remove();
            currentScreen = screen;
            currentScreen.Init();
            if (!hasFocus)
            {
                LostFocus();
            }
        }

        public void LostFocus()
        {
            hasFocus = false;
            if ((currentScreen as CompanyLogoScreen) == null)
            {
                mbFocus = new Entities.MessageBox("CLICK FOR FOCUS");
                currentScreen.AddEntity(mbFocus);
                currentScreen.paused = true;
            }
        }

        public void GotFocus()
        {
            hasFocus = true;
            if (mbFocus != null)
            {
                currentScreen.RemoveEntity(mbFocus);
                mbFocus = null;
                currentScreen.paused = false;
            }
        }
    }
}
